package com.eway.esb.service.rest;

import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.cxf.staxutils.DepthXMLStreamReader;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class SfyResponseTransformer extends AbstractTransformer{
	
	public SfyResponseTransformer(){
		super();
        this.registerSourceType(Object.class);
        this.setReturnClass(DepthXMLStreamReader.class);
	}

	@Override
	protected Object doTransform(Object obj, String s)
			throws TransformerException {
		// 把jdbc connection返回的list对象转换成xmlstreamreader流
		XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 
		XMLStreamReader xmlStreamReader;
		Document document = DocumentHelper.createDocument(); 
		Namespace ns=new Namespace("", "www.eway120.com");
		QName qname=new QName("Response", ns);
		Element root = document.addElement(qname);
		Element returnel=root.addElement("return");
		
		System.out.println("查找科室返回对象类型========"+obj.getClass());
			if(obj instanceof java.util.HashMap)
			{
				System.out.println("数据库导常。。。");
				returnel.addElement("returnCode").addText("204");
			}else
			{
				List returnList=(List)obj;
				if(returnList==null||returnList.size()==0){
					returnel.addElement("returnCode").addText("204");
				}else{
					returnel.addElement("returnCode").addText("0");
				}
				Iterator it=returnList.iterator();
				while(it.hasNext()){
					Map map=(Map)it.next();
					Iterator iter=map.keySet().iterator();
					Element dto=returnel.addElement("dto");
					while(iter.hasNext()){
						String key=(String)iter.next();
						String valueString="";
						try {
							valueString=new String((""+map.get(key)).toString().getBytes("ISO-8859-1"),"GBK");
						} catch ( Exception e) {
							// TODO: handle exception
						}
						
						dto.addElement(key).addText(valueString);
					}
					returnel.addElement(dto.getQName());
				}
				/*HashMap returnList=(HashMap)obj;  
				Document document = DocumentHelper.createDocument(); 
				Namespace ns=new Namespace("", "www.eway120.com");
				QName qname=new QName("Response", ns);
				Element root = document.addElement(qname);
				Element returnel=root.addElement("return");
				if(returnList==null||returnList.size()==0){
					returnel.addElement("returnCode").addText("204");
				}else{
					returnel.addElement("returnCode").addText("0");
				}
				Set set = returnList.entrySet();
				Iterator it=set.iterator();
				while(it.hasNext()){
					Map map=(Map)it.next();
					Iterator iter=map.keySet().iterator();
					Element dto=returnel.addElement("dto");
					while(iter.hasNext()){
						String key=(String)iter.next();
						dto.addElement(key).addText(""+map.get(key));
					}
					returnel.addElement(dto.getQName());
					
				}*/
				System.out.println("返回消息流内容："+document.asXML());
			}
		try {
			xmlStreamReader = inputFactory .createXMLStreamReader(new StringReader(document.asXML()));
			DepthXMLStreamReader reader=new DepthXMLStreamReader(xmlStreamReader);
			return reader;
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}

}
